//
//  RecordViewController.m
//  Runone
//
//  Created by runone01 on 16/9/19.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "RecordViewController.h"
#import "GuestCarViewController.h"
#import "POPDatePickViewController.h"
#import "TwoPassengersACrisisController.h"
//#import "DateViewController.h"
#import "PromptUtil.h"
#import "CommonUtilityjudge.h"
#import "AppConstants.h"
#import "CarManrgerDatePickViewController.h"
#import "RODateHelper.h"
#import "NSDate+ROCaculateTime.h"
#import "EKDateTimePickerView.h"
#import "UIColor+Addition.h"
#import "NSDate+Extension.h"

@interface RecordViewController ()<UITextFieldDelegate,UIPopoverPresentationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *endBtn;

@property (weak, nonatomic) IBOutlet UIButton *beginBtn;

@property (nonatomic, strong) CarManrgerDatePickViewController *datePickVC;
@property (nonatomic,strong)GuestCarViewController *g;
@property (nonatomic,strong)POPDatePickViewController *popdate;

@property (nonatomic,strong)PromptUtil *prom;
@property (weak, nonatomic) IBOutlet UIButton *conirmBtn;
@property (weak, nonatomic) IBOutlet UIButton *Cancle;
@property (nonatomic, copy)   NSString *bDate;
@property (nonatomic, copy)   NSString *eDate;
@property (nonatomic, strong) NSDate *lastScrollDate;// 上一次滚动的时间
@end

@implementation RecordViewController

#pragma   ---------两客一危    历史界面

- (void)viewDidLoad {
    [super viewDidLoad];
    _prom=[[PromptUtil alloc] init];
    //_lastScrollDate = [NSDate date];
    self.preferredContentSize=CGSizeMake(330, 216);
    _priNo.text=self.str;
    _BeginTime.delegate=self;
    _endTime.delegate=self;
    //    [self addTimeBtn];
    
    _BeginTime.layer.borderColor = kSeparateColor.CGColor;
    _BeginTime.layer.borderWidth = 1.0f;
    _BeginTime.font              = KTitleTextFont;
    
    _endTime.layer.borderColor = kSeparateColor.CGColor;
    _endTime.layer.borderWidth = 1.0f;
    _endTime.font              = KTitleTextFont;
    
    _priNo.layer.borderColor = kSeparateColor.CGColor;
    _priNo.layer.borderWidth = 1.0f;
    _priNo.font              = KTitleTextFont;
}

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.view addSubview:_endBtn];
    [self.view addSubview:_beginBtn];
}
//开始时间
- (IBAction)beginTimeBtnClick:(id)sender {
    [self setUpDatePicker:self.BeginTime];
}

//弹出时间选择器
- (void) setUpDatePicker:(UITextField *)showTF {
    
    EKDateTimePickerView *datepicker = [[EKDateTimePickerView alloc]initWithDatePosition:EKDatePositionMiddel CompleteBlock:^(NSDate *startDate) {
        _lastScrollDate = startDate;
        showTF.text = [startDate stringWithOutTimeWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    }];
    datepicker.datePickerStyle = EKDateStyleShowYearMonthDayHourMinute;
    datepicker.themeColor      = [UIColor colorWithHexString:@"38b1dc"];
 //   NSLog(@"123%@",showTF.text);
    if (showTF.text != nil) {
        _lastScrollDate = nil;
        [datepicker scrollToSepicefyDate:[RODateHelper dateWithOutTimeZoneFromString:showTF.text withFormat:nil] animated:NO];
    }
   
    if (_lastScrollDate != nil) {
        [datepicker scrollToSepicefyDate:_lastScrollDate animated:NO];
    }
    [datepicker show];
}

//结束时间
- (IBAction)endTimeBtnClick:(id)sender {
    [self setUpDatePicker:self.endTime];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textFiel {
    return NO;
}

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//两客一危历史 取消按钮
- (IBAction)cancle:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//是否是合法车牌号
- (BOOL)validateCarNo:(NSString*)carNo {
    
    NSString *carRegex =@"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    //    NSLog(@"carTest is %@",carTest);
    if ([carTest evaluateWithObject:carNo]) {
        return YES;
    }else{
        return NO;
    }
}
//确定按钮
- (IBAction)confim:(UIButton *)sender {
    
    if (_priNo.text.length==0) {
        [_prom alert:@"车牌号码不能为空"];
        return;
    }
    if (stringIsEmpty(_BeginTime.text)) {
        [_prom alert:@"请选择开始时间"];
        return ;
    }
    if (stringIsEmpty(_endTime.text)) {
        [_prom alert:@"请选择结束时间"];
        return ;
    }
    NSLog(@"b%@ e%@",self.BeginTime.text, self.endTime.text);
    _bDate = self.BeginTime.text;
    _eDate = self.endTime.text;
    NSDate *bDate = [RODateHelper dateWithOutTimeZoneFromString:self.BeginTime.text withFormat:nil];
    NSDate *eDate = [RODateHelper dateWithOutTimeZoneFromString:self.endTime.text withFormat:nil];
    NSInteger beginInter = [bDate compareToCurrentDate];
    if (beginInter == 0) {
        [_prom alert:@"开始时间不能等于当前时间"];
        return ;
    }
    if (beginInter == 1) {
        [_prom alert:@"开始时间不能大于当前时间"];
        return ;
    }
    
    NSInteger endInter = [eDate compareToCurrentDate];
    if (endInter == 1) {
        [_prom alert:@"结束时间不能大于当前时间"];
        return ;
    }
    
    NSInteger beginAndEndCom = [bDate compareToToDate:eDate];
    if (beginAndEndCom == 0) {
        [_prom alert:@"开始时间不能等于结束时间"];
        return ;
    }
    if (beginAndEndCom == 1) {
        [_prom alert:@"开始时间不能大于结束时间"];
        return ;
    }
    // NSLog(@"%@ %@",_beginDate,_endDate);
    BOOL is24Hour = [bDate TwoDateIsIn24HourTODate:eDate];
    if (!is24Hour) {
        [_prom alert:@"结束时间不能超过开始时间24小时"];
        return ;
    }

        [self dismissViewControllerAnimated:YES completion:^{
            self.pushBlock(self.priNo.text, _bDate,_eDate);
        }];
}

@end
